//
//  HomeNewMorePicCell.m
//  ioschishan
//
//  Created by 马田成 on 2017/7/28.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "HomeNewMorePicCell.h"
@interface HomeNewMorePicCell ()

{
    NSIndexPath *_cellIndexPath;  // 当前Cell的下标
}

@end
@implementation HomeNewMorePicCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self layoutUI];
    }
    return self;
}

- (void)setCellContent:(NSString *)contentStr andIsShow:(BOOL)isShow andCellIndexPath:(NSIndexPath *)indexPath
{
    _textsLabel.text = contentStr;
    _cellIndexPath = indexPath;
    
    CGRect rect = [_textsLabel.text boundingRectWithSize:CGSizeMake(BOUNDS.size.width-30, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil];
    if (rect.size.height > 52) {
        // 文字大于三行，显示展开收起按钮
        self.moreBtn.hidden = NO;
        if (isShow) {
            _textsLabel.numberOfLines = 0;
            [_textsLabel mas_remakeConstraints:^(MASConstraintMaker *make){
                make.left.mas_equalTo(self.contentView.mas_left).offset(15);
                make.top.mas_equalTo(_headImage.mas_bottom).offset(10);
                make.width.mas_equalTo(BOUNDS.size.width-30);
                make.height.mas_equalTo(rect.size.height+1);
            }];
        } else {
            [_textsLabel mas_remakeConstraints:^(MASConstraintMaker *make){
                make.left.mas_equalTo(self.contentView.mas_left).offset(15);
                make.top.mas_equalTo(_headImage.mas_bottom).offset(10);
                make.width.mas_equalTo(BOUNDS.size.width-30);
                make.height.mas_equalTo(52);
            }];
        }
    } else {
        // 文字小于三行，隐藏展开收起按钮
        _textsLabel.numberOfLines = 3;
        self.moreBtn.hidden = YES;
        [_textsLabel mas_remakeConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(self.contentView.mas_left).offset(15);
            make.top.mas_equalTo(_headImage.mas_bottom).offset(10);
            make.width.mas_equalTo(BOUNDS.size.width-30);
            make.height.mas_equalTo(rect.size.height+1);  // 由于系统计算的那个高度有时候会有1像素到2像素的误差，所以这里把高度+1
        }];
    }
}

#pragma mark - 显示更多按钮点击事件
- (void)moreButtonClicked
{
    self.moreBtn.selected = !self.moreBtn.selected;
    if (self.moreBtn.selected) {
        //        [self.moreBtn setImage:[UIImage imageNamed:@"xiala"] forState:UIControlStateNormal];
        [_moreBtn setTitle:@"收起全文" forState:UIControlStateNormal];
    }else{
        //        [self.moreBtn setImage:[UIImage imageNamed:@"shq"] forState:UIControlStateNormal];
        [_moreBtn setTitle:@"展开全文" forState:UIControlStateNormal];
    }
    
    // 记录当前按钮的选中状态，并传递给Controller
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:[NSNumber numberWithInteger:_cellIndexPath.row] forKey:@"row"];
    [dic setObject:[NSNumber numberWithBool:self.moreBtn.selected] forKey:@"isShow"];
    
    
    // 协议回调，改变Controller中存放Cell展开收起状态的字典
    if (self.delegate && [self.delegate respondsToSelector:@selector(remarksCellShowContrntWithDic:andCellIndexPath:)]) {
        [self.delegate remarksCellShowContrntWithDic:dic andCellIndexPath:_cellIndexPath];
    }
}

- (void)layoutUI
{
    
    self.headImage = [[UIImageView alloc] init];
    [self.contentView addSubview:self.headImage];
    [self.headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(15);
        make.top.mas_equalTo(self.contentView.mas_top).offset(15);
        make.height.offset(36);
        make.width.offset(36);
        
    }];
    self.headImage.image = [UIImage imageNamed:@"图层-19"];
    self.headImage.layer.cornerRadius = 18;
    self.headImage.layer.masksToBounds = YES;
    _infolable = [[UILabel alloc]init];
    _infolable.text = @"天边彩虹";
    _infolable.textColor = CLColor(74, 74, 74);
    _infolable.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_infolable];
    [_infolable mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(self.headImage.mas_right).offset(10);
        make.top.mas_equalTo(self.contentView.mas_top).offset(17);
        make.height.mas_equalTo(12);
    }];
    
    self.labelTeacher = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelTeacher];
    _labelTeacher.backgroundColor = getColor(@"3A95FF");
    _labelTeacher.text = @"官方";
    _labelTeacher.textColor = [UIColor whiteColor];
    
    _labelTeacher.font = [UIFont systemFontOfSize:8];
    _labelTeacher.textAlignment = NSTextAlignmentCenter;
    [_labelTeacher mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).with.offset(19);
        make.left.equalTo(_infolable.mas_right).with.offset(5);
        make.height.offset(12);
        make.width.offset(30);
        
    }];
    
    _labelOfTime = [[UILabel alloc] init];
    [self.contentView addSubview:_labelOfTime];
    _labelOfTime.textColor = getColor(@"9B9B9B");
    _labelOfTime.backgroundColor = [UIColor whiteColor];
    _labelOfTime.text = @"9分钟前";
    _labelOfTime.font = [UIFont systemFontOfSize:12];
    [_labelOfTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_infolable.mas_bottom).with.offset(8);
        make.left.equalTo(self.headImage.mas_right).with.offset(10);
        make.height.offset(12);
        
        
    }];
    
    _textsLabel = [[UILabel alloc]init];
    //    _textsLabel.lineBreakMode = NSLineBreakByCharWrapping;
    _textsLabel.numberOfLines = 3;
    _textsLabel.font = [UIFont systemFontOfSize:12];
    _textsLabel.textColor = CLColor(74, 74, 74);
    [self.contentView addSubview:_textsLabel];
    
    _moreBtn = [[UIButton alloc]init];
    //    [_moreBtn setImage:[UIImage imageNamed:@"shq"] forState:UIControlStateNormal];
    _moreBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [_moreBtn setTitle:@"展开全文" forState:UIControlStateNormal];
    [_moreBtn setTitleColor:CLColor(153, 153, 153) forState:UIControlStateNormal];
    
    //    [_moreBtn setImage:[UIImage imageNamed:@"xiala"] forState:UIControlStateHighlighted];
    [_moreBtn setTitle:@"收起全文" forState:UIControlStateHighlighted];
    [_moreBtn addTarget:self action:@selector(moreButtonClicked) forControlEvents:UIControlEventTouchUpInside];
     _moreBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_moreBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [self.contentView addSubview:_moreBtn];
    [_moreBtn mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(self.contentView.mas_left);
        make.top.mas_equalTo(_textsLabel.mas_bottom).offset(-10);
        make.width.mas_equalTo(BOUNDS.size.width-10);

        make.height.mas_equalTo(30);
    }];
    
    self.picImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"3479.jpg_wh1200"]];
    [self.contentView addSubview:_picImage];
    [self.picImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_moreBtn.mas_bottom).with.offset(5);
        make.left.equalTo(self.contentView).with.offset(15);
        make.height.offset(80);
        make.right.equalTo(self.contentView).with.offset(-15);
        
    }];
    
    UIView *viewG = [[UIView alloc] init];
    [self.contentView addSubview:viewG];
    viewG.backgroundColor = getColor(@"F5F5F5");
    [viewG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.height.offset(8);
        make.bottom.equalTo(self.contentView);
        
        
    }];

}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
